<template><div><h2 id="权限控制" tabindex="-1"><a class="header-anchor" href="#权限控制"><span>权限控制</span></a></h2>
<p><code v-pre>Dcat Admin</code>已经内置了<code v-pre>RBAC</code>权限控制模块，展开左侧边栏的<code v-pre>Auth</code>，下面有用户、角色、权限三项的管理面板，权限控制的使用如下：</p>
<h2 id="路由控制" tabindex="-1"><a class="header-anchor" href="#路由控制"><span>路由控制</span></a></h2>
<p>在<code v-pre>Dcat Admin</code>中，权限和路由是绑定在一起的，在编辑权限页面里面设置当前权限能访问的路由，在<code v-pre>HTTP方法</code>select框中选择访问路由的方法，在<code v-pre>HTTP路径</code>中填写能访问的路径。</p>
<p>比如要添加一个权限，该权限可以以<code v-pre>GET</code>方式访问路径<code v-pre>/admin/users</code>，那么<code v-pre>HTTP方法</code>选择<code v-pre>GET</code>，<code v-pre>HTTP路径</code>填写<code v-pre>/users</code>。</p>
<p>如果要访问前缀是<code v-pre>/admin/users</code>的所有路径，那么<code v-pre>HTTP路径</code>填写<code v-pre>/users*</code>；如果要访问的是编辑页，那么<code v-pre>HTTP路径</code>填写<code v-pre>/users/*/edit</code>；如果多个路径中每个路径的方法不同，那么<code v-pre>HTTP路径</code>填写<code v-pre>GET:users/*</code>。</p>
<blockquote>
<p>Since <code v-pre>v1.7.0</code></p>
</blockquote>
<p>如果上述的方法不能满足需求，<code v-pre>HTTP路径</code>还支持填写路由别名，如<code v-pre>admin.users.show</code></p>
<h2 id="禁用权限功能" tabindex="-1"><a class="header-anchor" href="#禁用权限功能"><span>禁用权限功能</span></a></h2>
<blockquote>
<p>Since <code v-pre>v1.5.3</code></p>
</blockquote>
<p>把<code v-pre>admin.auth.enable</code>配置参数的值设置为<code v-pre>false</code>可以完全禁用内置的权限系统。</p>
<h2 id="跳过权限验证" tabindex="-1"><a class="header-anchor" href="#跳过权限验证"><span>跳过权限验证</span></a></h2>
<p>可以把需要跳过权限验证的接口加入到配置文件<code v-pre>admin.permission.except</code>参数中</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token string single-quoted-string">'permission'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// Whether enable permission.</span></span>
<span class="line">        <span class="token string single-quoted-string">'enable'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// All method to path like: auth/users/*/edit</span></span>
<span class="line">        <span class="token comment">// or specific method to path like: get:auth/users.</span></span>
<span class="line">        <span class="token string single-quoted-string">'except'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'/'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'auth/login'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'auth/logout'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'auth/setting'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="页面控制" tabindex="-1"><a class="header-anchor" href="#页面控制"><span>页面控制</span></a></h2>
<p>如果你要在页面中控制用户的权限，可以参考下面的例子</p>
<h3 id="场景1" tabindex="-1"><a class="header-anchor" href="#场景1"><span>场景1</span></a></h3>
<p>比如现在有一个场景，对文章发布模块做权限管理，以创建文章为例</p>
<p>首先创建一项权限，进入<code v-pre>http://localhost/admin/auth/permissions</code>，权限标识（slug）填写<code v-pre>create-post</code>，权限名称填写<code v-pre>创建文章</code>，这样权限就创建好了。</p>
<p>第二步可以把这个权限直接附加给个人或者角色，在用户编辑页面可以直接把上面创建好的权限附加给当前编辑用户，也可以在编辑角色页面附加给某个角色。</p>
<p>第三步，在创建文章控制器里面添加控制代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>Permission</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 检查权限，有create-post权限的用户或者角色可以访问创建文章页面</span></span>
<span class="line">        <span class="token class-name static-context">Permission</span><span class="token operator">::</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'create-post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这样就完成了一个页面的权限控制。</p>
<h3 id="场景2" tabindex="-1"><a class="header-anchor" href="#场景2"><span>场景2</span></a></h3>
<p>如果你要在表格中控制用户对元素的显示，那么需要先定义两个权限，比如权限标示<code v-pre>delete-image</code>、和<code v-pre>view-title-column</code>分别用来控制有删除图片的权限和显示某一列的权限，把这两个权限赋给你设置的角色，然后在grid中加入代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">actions</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$actions</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 没有`delete-image`权限的角色不显示删除按钮</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'delete-image'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$actions</span><span class="token operator">-></span><span class="token function">disableDelete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 只有具有`view-title-column`权限的用户才能显示`title`这一列</span></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view-title-column'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">column</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="相关方法" tabindex="-1"><a class="header-anchor" href="#相关方法"><span>相关方法</span></a></h2>
<p>获取当前用户对象</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>获取当前用户id</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>获取用户角色</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">roles</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>获取用户的权限</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">permissions</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>用户是否有某个角色</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isRole</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'developer'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>是否有某个权限</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'create-post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>是否没有某个权限</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cannot</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'delete-post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>是否是超级管理员</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdministrator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>是否是其中的角色</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">inRoles</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'editor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="权限中间件" tabindex="-1"><a class="header-anchor" href="#权限中间件"><span>权限中间件</span></a></h2>
<p>可以在路由配置上结合权限中间件来控制路由的权限</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"></span>
<span class="line"><span class="token comment">// 允许administrator、editor两个角色访问group里面的路由</span></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">group</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'middleware'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'admin.permission:allow,administrator,editor'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$router</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$router</span><span class="token operator">-></span><span class="token function">resource</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token class-name static-context">UserController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 禁止developer、operator两个角色访问group里面的路由</span></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">group</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'middleware'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'admin.permission:deny,developer,operator'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$router</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$router</span><span class="token operator">-></span><span class="token function">resource</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token class-name static-context">UserController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 有edit-post、create-post、delete-post三个权限的用户可以访问group里面的路由</span></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">group</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'middleware'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'admin.permission:check,edit-post,create-post,delete-post'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$router</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$router</span><span class="token operator">-></span><span class="token function">resource</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token class-name static-context">PostController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>权限中间件和其它中间件使用方法一致。</p>
<h2 id="为何配置了角色和权限-依然提示无权访问" tabindex="-1"><a class="header-anchor" href="#为何配置了角色和权限-依然提示无权访问"><span>为何配置了角色和权限，依然提示无权访问？</span></a></h2>
<p>这个原因可能是由于权限的<code v-pre>URL</code>路径配置错误导致的，正确的包含增删改查功能的<code v-pre>URL</code>配置应该是<code v-pre>auth/users*</code>这样的，如果你配置成了<code v-pre>auth/users/*</code>，那么就会提示无权访问。</p>
<blockquote>
<p>另外标签表单填写自定义URL有两种方法：一种是选中后按<code v-pre>删除键</code>进行更改；另一种是填写后按<code v-pre>空格键</code> + <code v-pre>回车键</code>。</p>
</blockquote>
</div></template>


